package com.itboye.mengwu.app.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.GoodsListModel;
import com.itboye.mengwu.data.model.TestModel;

/** GirdView 数据适配器 */
public class GoodsListAdapter extends BaseAdapter {
	Context context;

	List<GoodsListModel.GoodsList> list;

	public GoodsListAdapter(Context _context,
			List<GoodsListModel.GoodsList> _list) {
		this.list = _list;
		this.context = _context;
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? 0 : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.goods_list_item, null);
			/**
			 * item_goodscomment_nickname,item_goodscomment_introduction,
			 * item_goodscomment_goods_date
			 * ,item_goodscomment_goods_color,item_goodscomment_goods_size
			 */
			holder.goods_list_goodstv = (TextView) convertView
					.findViewById(R.id.goods_list_goodstv);
			holder.old_price = (TextView) convertView
					.findViewById(R.id.old_price);
			holder.new_price = (TextView) convertView
					.findViewById(R.id.new_price);
			holder.price_rebate = (TextView) convertView
					.findViewById(R.id.price_rebate);
			holder.goods_list_image = (ImageView) convertView
					.findViewById(R.id.goods_list_image);
			holder.push_goods_logo = (ImageView) convertView
					.findViewById(R.id.push_goods_logo);

			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.goods_list_goodstv.setText(list.get(position).getName());
		holder.new_price.setText(list.get(position).getPrice());
		holder.old_price.setText(list.get(position).getOri_price());
		// double new_p = Double.parseDouble(list.get(position).getPrice());
		// double old_p = Double.parseDouble(list.get(position).getOri_price());
		if (list.get(position).getDiscount() != null
				|| list.get(position).getDiscount() != "") {
			double discount = Double.parseDouble(list.get(position)
					.getDiscount());
			DecimalFormat df = new DecimalFormat("######0.0");
			String rebate = df.format((discount) * 10);

			holder.price_rebate.setText(rebate);

			if (rebate.equals("10.0")) {
				holder.push_goods_logo.setVisibility(View.GONE);
			} else {
				holder.push_goods_logo.setVisibility(View.VISIBLE);
			}
		} else {
			holder.push_goods_logo.setVisibility(View.GONE);
		}

		// holder.goods_list_goodstv.setText(list.get(position).getTotal_sales());
		XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
				+ list.get(position).getImg(), holder.goods_list_image);
		return convertView;
	}

	class Holder {
		ImageView goods_list_image, push_goods_logo;
		TextView old_price, price_rebate;
		TextView goods_list_goodstv, item_goodscomment_introduction,
				item_goodscomment_goods_date, new_price,
				item_goodscomment_goods_size;
	}
}